package berlinatyourfingertips.apis.twitter;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;

public class TwitterRequester extends TimerTask
{
	private static TwitterRequester _instance;
	
	private Twitter _twitt;
	private Timer _timer;
	
	private long _lastID = 1;
	
	public static void main(String[] args)
	{
		_instance = new TwitterRequester();
    }
	
	public TwitterRequester()
	{
		this._twitt = new Twitter("codesterTobster", "herbertram");
		
		this._timer = new Timer();
		this._timer.schedule(this, 0, 2000);
	}

	@Override
	public void run()
	{
		 try
        {
			List<Status> statuses = _twitt.getPublicTimeline((int)this._lastID);
            for(Status status : statuses)
            {
                System.out.println(status.getUser().getName() + ": " + status.getText());
                System.out.println(status.getUser().getLocation());
                System.out.println("");
            }
            
            this._lastID = statuses.get(statuses.size() - 1).getId();
            System.out.println(this._lastID);
        }
        catch (TwitterException te)
        {
            System.out.println("Failed to get timeline: " + te.getMessage());
            System.exit(-1);
        }
	}
}
